var wxRequest = require('../../utils/wxRequest')
var Common = require('../../utils/common');
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    notPayBillList: [],
    totalFee: 0,
    billIdStr: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var _that = this;
    var url = app.globalData.domain + "/client/community/getMrRoom";
    var parm = {
      token: wx.getStorageSync("token"),
      idCommunity: wx.getStorageSync("idCommunity")
    }
    wxRequest.postRequest(url, parm).then(res => {
      console.log("获取当前房屋", res.data);
      _that.setData({
        mrRoom: res.data
      });

      var getNotPayByRoomUrl = app.globalData.domain + "/client/propertyBill/getOverdueByRoom";
      var parm = {
        token: wx.getStorageSync("token"),
        idRoom: res.data.idRoom
      }
      wxRequest.postRequest(getNotPayByRoomUrl, parm).then(result => {
        console.log("获取未支付账单", result);
        result.data[0].check = true;
        _that.setData({
          notPayBillList: result.data,
          billIdStr: result.data[0].id,
        });
        if (result.data.length > 0) {
          var totalFee = 0;
          var billIdArr = [];
          var billIdStr = "";
          for (var index in result.data) {
            totalFee += result.data[index].talFee;
            billIdArr[index] = result.data[index].id;
            result.data[index].check = false;
            result.data[index].talFee = result.data[index].talFee.toFixed(2);
          }

          

          this.setData({
            totalFee: totalFee.toFixed(2),
            billIdStr: billIdArr.join(",")
          });
        }

      }, error => {
        Common.showToast(error);
      });
    }, error => {
      Common.showToast(error);
    });
  },


  check: function(e) {
    let billIdStr = [];
    let data = this.data.notPayBillList;
    let index = e.currentTarget.dataset.index;
    console.log(index);

    if (data[index].check){
      if (index == 0){
        data[index].check = true;
      }else{
        data[index].check = false;
      }
    }else{
      if (data[index-1].check == false){
        app.toast('请结清上一个月的账单');
      }else{
        data[index].check = true;
      }
    }
    
    for (let i in data) {
      if (data[i].check){
        billIdStr.push(data[i].id)
      }
    }


    this.setData({
      notPayBillList:data,
      billIdStr: billIdStr.join(",")
    });


  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

})